<?php
	require_once("BaseClass.php");

	class FileItem
	{
		public $name;
		public $crc;
		public $size;
	}


	class CRCFile extends BaseClass
	{
		public $dir = array();
		public $outPutName = "";
		public $allFiles = array();
		public $items = array();
		function __construct($dir,$outPutName)
		{
			echo "PackFile __construct" . "\r\n";
			$this->dir = explode("|",$dir);
			$this->outPutName = $outPutName;
			foreach ($this->dir as $key => $value) {
				$tmp = $this->scanfiles($value);
				foreach ($tmp as $keyx => $valuey) {
					echo $valuey . "\r\n";
					array_push($this->allFiles, $valuey);
				}
			}
			$this->init();
		}
		function __destruct()
		{
			echo "PackFile __destruct" . "\r\n";
		}

		function init()
		{
			foreach ($this->allFiles as $key => $value)
			{
				$item = new FileItem();
				$item->name = $value;
				$item->crc = sprintf('"%s"',substr(md5(file_get_contents($value)),8,16));
				//$item->crc = substr(md5(file_get_contents($value)),8,16);
				$item->size = filesize($value);
				array_push($this->items, $item);
			}
		}

		function saveCrcFile()
		{
			$str = "local " . 't' . " = {";
			foreach ($this->items as $key => $value) {
				$str .= sprintf("\r\t[%s%s%s] = {crc = %s,size = %s},",'"',$value->name,'"',$value->crc,$value->size);
			}
			$str = substr($str,0,strlen($str)-1);
			$str .= "\r\n} \r\nreturn ". 't' .";";
			file_put_contents($this->outPutName,$str);
		}
		function delObj()
		{
			foreach ($this->items as $key => $value) {
				unset($value);
			}
		}
	}

	$input = $argv[2];
	$output = $argv[4];
//	$crc = new CRCFile("../res/","../res/crc.lua");
	$crc = new CRCFile($input,$output);
	$crc->saveCrcFile();
	$crc->delObj();
	unset($crc);
?>